package com.smartleanx.module.hrm.service.employee;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.smartleanx.framework.common.util.collection.CollectionUtils;
import com.smartleanx.module.bpm.enums.task.BpmProcessInstanceStatusEnum;
import com.smartleanx.module.hrm.controller.admin.employee.vo.EmployeeLeaveReqVO;
import com.smartleanx.module.hrm.dal.dataobject.employee.EmployeeTransferRecordDO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;

import com.smartleanx.module.hrm.dal.dataobject.employee.EmployeeSeparationRecordDO;
import com.smartleanx.framework.common.util.object.BeanUtils;

import com.smartleanx.module.hrm.dal.mysql.employee.EmployeeSeparationRecordMapper;

import java.util.*;
import java.util.stream.Collectors;

import static com.smartleanx.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.smartleanx.module.hrm.enums.ErrorCodeConstants.EMPLOYEE_SEPARATION_INFO_NOT_EXISTS;

/**
 * 离职信息 Service 实现类
 *
 * @author tengcj
 */
@Service
@Validated
public class EmployeeSeparationRecordServiceImpl implements EmployeeSeparationRecordService {

    @Resource
    private EmployeeSeparationRecordMapper employeeSeparationRecordMapper;

    @Override
    public Long createEmployeeSeparationRecord(EmployeeLeaveReqVO createReqVO) {
        createReqVO.setId(null);
        // 插入
        EmployeeSeparationRecordDO employeeSeparationRecord = BeanUtils.toBean(createReqVO, EmployeeSeparationRecordDO.class);
        employeeSeparationRecordMapper.insert(employeeSeparationRecord);
        // 返回
        return employeeSeparationRecord.getId();
    }

    @Override
    public void updateEmployeeSeparationRecord(EmployeeLeaveReqVO updateReqVO) {
        // 校验存在
        validateEmployeeSeparationRecordExists(updateReqVO.getId());
        // 更新
        EmployeeSeparationRecordDO updateObj = BeanUtils.toBean(updateReqVO, EmployeeSeparationRecordDO.class);
        employeeSeparationRecordMapper.updateById(updateObj);
    }

    @Override
    public void updateEmployeeSeparation(EmployeeSeparationRecordDO recordDO) {
        // 校验存在
        validateEmployeeSeparationRecordExists(recordDO.getId());
        employeeSeparationRecordMapper.updateById(recordDO);
    }

    @Override
    public void deleteEmployeeSeparationRecord(Long id) {
        // 校验存在
        validateEmployeeSeparationRecordExists(id);
        // 删除
        employeeSeparationRecordMapper.deleteById(id);
    }

    private void validateEmployeeSeparationRecordExists(Long id) {
        if (employeeSeparationRecordMapper.selectById(id) == null) {
            throw exception(EMPLOYEE_SEPARATION_INFO_NOT_EXISTS);
        }
    }

    @Override
    public EmployeeSeparationRecordDO getEmployeeSeparationRecord(Long id) {
        return employeeSeparationRecordMapper.selectById(id);
    }

    @Override
    public List<EmployeeSeparationRecordDO> getToBeSeparatedList() {
        return employeeSeparationRecordMapper.getToBeSeparatedList();
    }

    @Override
    public List<EmployeeSeparationRecordDO> getSeparatedList(Long employeeId) {
        return employeeSeparationRecordMapper.getSeparatedList(employeeId);

    }

    @Override
    public List<EmployeeSeparationRecordDO> getSeparationRecordList(Collection<Long> employeeIdList) {
        if (CollUtil.isEmpty(employeeIdList)) {
            return Collections.emptyList();
        }
        return employeeSeparationRecordMapper.selectListByEmployeeId(employeeIdList);
    }

    @Override
    public List<EmployeeSeparationRecordDO> getSeparationRecordList() {
        List<EmployeeSeparationRecordDO> list = employeeSeparationRecordMapper.selectList();
        if (CollUtil.isEmpty(list)) {
            return Collections.emptyList();
        }
        // 按员工分类，取最后一条
        Map<Long, List<EmployeeSeparationRecordDO>> listMap = CollectionUtils.convertMultiMap(list, EmployeeSeparationRecordDO::getEmployeeId);
        List<EmployeeSeparationRecordDO> recordList = new ArrayList<>();
        for (Long key : listMap.keySet()) {
            List<EmployeeSeparationRecordDO> collect = listMap.get(key).stream().sorted(Comparator.comparing(EmployeeSeparationRecordDO::getId).reversed()).collect(Collectors.toList());
            recordList.add(collect.get(0));
        }
        return recordList;
    }

    @Override
    public void deleteEmployeeSeparationRecordNew(Long id) {
        employeeSeparationRecordMapper.deleteById(id);
    }
}
